<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * -----------------------------------------------------
 *
 * manage role
 *
 * @package     controllers
 * @subpackage  
 * @category    role
 * @author      ChenFu
 * @since       2013-11-29 version 1.0       
 * -----------------------------------------------------    
 */

class Role extends CI_Controller{

	public function __construct(){
		parent::__construct();
		$this->load->model('role_model'); // 加载角色模型类
		$this->load->model('menu_model'); // 加载菜单模型类
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation'); // 表单验证类
	  	check_login(); // 检查是否登录
	}

	/**
	 * 
	 * @return [type] [list]
	 */
	public function index()
	{
		$total = $this->role_model->get_num_rows(); // 获取角色个数
		if($total != 0){
			// 加载分页类
			$arr = array('page_total_num' => $total,'every_page_num'=>10,'page_url_name'=>'page_id');
			$this->load->library('page',$arr);
			$data['pager']	= $this->page->pages_all();
			$data['arr'] 	= $this->role_model->get_all_role($this->page->limit);
		}else{
			$data['error'] = '暂无角色!';
		}
		$this->load->view('admin/role/list',$data);
	}
	/**
	 * 增加角色
	 */
	public function insert()
	{
		$data['rolesOneArr'] = $this->menu_model->get_all_pop(0);
		foreach($data['rolesOneArr'] as $rolesOneRows){
			$sonArr = $this->menu_model->get_all_pop($rolesOneRows['menu_id']);
			$rolesSonArr[$rolesOneRows['menu_id']] = $sonArr;
			if($sonArr){
				foreach ($sonArr as $key => $value) {
					$rolesSonArr[$rolesOneRows['menu_id']][$key]['sonArr'] = $this->menu_model->get_all_pop($value['menu_id']);
				}
			}
		}
		$data['rolesSonArr'] = $rolesSonArr;		
		if ($this->form_validation->run('role') == FALSE){
		   $this->load->view('admin/role/add',$data);
		}else{
			if(!empty($_POST['role_name'])){
				$_insert['role_name'] = $_POST['role_name'];
				unset($_POST['role_name']);
				unset($_POST['x']);
				unset($_POST['y']);
				$_insert['role_popedom'] = implode('|',$_POST).'|';
 				if($this->role_model->insert($_insert)){
					$this->sys_message->skip('增加角色成功!',site_url('manage/role'));
				}else{
					$this->sys_message->show_error('增加角色失败!');
				}
			}
		}
	}


    /**
     * 编辑角色
     */
    public function edit()
    {  
		// 默认值
		$data['r_val'] = $this->role_model->get_role_info_by_id($this->input->get('r_id'));
        // 加载权限
		$data['rolesOneArr'] = $this->menu_model->get_all_pop(0);
		foreach($data['rolesOneArr'] as $rolesOneRows){
			$sonArr = $this->menu_model->get_all_pop($rolesOneRows['menu_id']);
			$rolesSonArr[$rolesOneRows['menu_id']] = $sonArr;
			if($sonArr){
				foreach ($sonArr as $key => $value) {
					$rolesSonArr[$rolesOneRows['menu_id']][$key]['sonArr'] = $this->menu_model->get_all_pop($value['menu_id']);
				}
			}
		}
		$data['rolesSonArr'] = $rolesSonArr;		
        if ($this->form_validation->run('role') == FALSE){
		  $this->load->view('admin/role/edit',$data);
		}else{
			if(!empty($_POST['role_name'])){
				$arr = $this->input->post();
				$_update['role_name'] = $arr['role_name'];
				unset($arr['role_name']);
				unset($arr['x']);
				unset($arr['y']);
				$_update['role_popedom'] = implode('|',$arr).'|';
				if($this->role_model->update($_update,$_GET['r_id']))
				{
					 $this->sys_message->skip('编辑角色成功',site_url('manage/role'));
				}else{
					 $this->sys_message->show_error('角色添加失败,请重新添加');
				}
			}
		}
    }
    /**
     * 删除角色
     */
    public function delete()
    {
        if($this->role_model->delete($this->input->get('r_id'))){
        	 $this->sys_message->skip('角色删除成功',site_url('manage/role'));
        }else{
        	$this->sys_message->show_error('角色删除失败');
        }
    }
    /**
     * 查看角色权限
     */
    public function info()
    {
		// 默认值
		$data['r_val'] = $this->role_model->get_role_info_by_id($this->input->get('r_id'));
    	// 加载权限
		$data['rolesOneArr'] = $this->menu_model->get_all_pop(0);
		foreach($data['rolesOneArr'] as $rolesOneRows){
			$sonArr = $this->menu_model->get_all_pop($rolesOneRows['menu_id']);
			$rolesSonArr[$rolesOneRows['menu_id']] = $sonArr;
			if($sonArr){
				foreach ($sonArr as $key => $value) {
					$rolesSonArr[$rolesOneRows['menu_id']][$key]['sonArr'] = $this->menu_model->get_all_pop($value['menu_id']);
				}
			}
		}
		$data['rolesSonArr'] = $rolesSonArr;		
        $this->load->view('admin/role/info',$data);
    }
    
}